﻿Imports System.Data.SqlClient
Imports System.Data
Public Class wucProductList
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Me.IsPostBack Then
            Me.LoadProducts()
        End If
    End Sub

    Public Sub LoadProducts()
        'Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTemp.mdf;Integrated Security=True;User Instance=True"
        Dim conStr As String = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123"

        Dim sqlcon As New SqlClient.SqlConnection(conStr)

        Try
            sqlcon.Open()

            Dim strSQL As String = " select * from Products "

            Dim dbcomm As New Data.SqlClient.SqlCommand(strSQL, sqlcon)

            Dim da As New Data.SqlClient.SqlDataAdapter
            da.SelectCommand = dbcomm

            'Dim dbreader = dbcomm.ExecuteReader()

            Dim dt As New DataTable
            da.Fill(dt)
            'dt.Load(dbreader)

            Me.DataList1.DataSource = dt
            Me.DataList1.DataBind()

            da.Dispose()
            sqlcon.Close()
        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        Finally
            sqlcon.Close()
        End Try

    End Sub

    Private Sub RemoveRecord(ByVal idProduct As String)

        'Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTemp.mdf;Integrated Security=True;User Instance=True"
        Dim conStr As String = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123"

        Dim sqlcon As New SqlClient.SqlConnection(conStr)
        Try
            sqlcon.Open()

            Dim strSQL As String = "Delete from Products where idProducts=" & idProduct
            Dim dbcomm As New Data.SqlClient.SqlCommand(strSQL, sqlcon)

            Dim da As New Data.SqlClient.SqlDataAdapter
            da.SelectCommand = dbcomm

            dbcomm.ExecuteNonQuery()
            da.Dispose()
            sqlcon.Close()
            Response.Redirect(Me.Request.Path)
        Catch ex As Exception
            Response.Write(ex.Message.ToString)
        Finally
            sqlcon.Close()
        End Try
    End Sub

    Private Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
        Me.HideOpenedControls()
        Dim wucProductMaint1 As wucProductMaint = e.Item.FindControl("wucProductMaint1")
        If e.Item.ItemType = ListItemType.Footer Then
            Dim btnNew As Button = e.Item.FindControl("btnNew")
            btnNew.Visible = Not e.CommandName.Equals("NewRegister")
            wucProductMaint1.HeaterText = "New Register"
            wucProductMaint1.Visible = e.CommandName.Equals("NewRegister")
        Else
            Dim lblID As Label = e.Item.FindControl("lblID")
            If e.CommandName = "Edit" Then
                wucProductMaint1.IdProduct = lblID.Text
                wucProductMaint1.HeaterText = "Edit"
                wucProductMaint1.Visible = True
            ElseIf e.CommandName = "Remove" Then
                Me.RemoveRecord(lblID.Text)
            Else
                wucProductMaint1.Visible = False
            End If

        End If
    End Sub

    Private Sub HideOpenedControls()
        For Each ditem As DataListItem In DataList1.Items
            Dim wucProductMaint1 As wucProductMaint = ditem.FindControl("wucProductMaint1")
            wucProductMaint1.Visible = False
        Next
    End Sub
End Class